<?php

function printYear($date)
{
	return "<td class=\"EventReportYear\" colspan=\"4\">".$date->format("Y")."</td></tr><tr>";
}

function printMonth($date)
{
	return "<td class=\"EventReportMonth\" colspan=\"4\">".$date->format("F")."</td></tr><tr>";
}

function printDay($date)
{
	return "<td class=\"EventReportDay\" colspan=\"4\">".$date->format("j l")."</td></tr><tr>";
}


// echo dirname(__FILE__)."\n";

$topdirectory=dirname(__FILE__)."/../../MSWebGUI";
include $topdirectory.'/FileList.php';

$topdirectory=dirname(__FILE__)."/..";
include $topdirectory.'/FileList.php';
//echo $topdirectory.'/FileList.php';
include $topdirectory.'/model/classes/events/EventsResultSet.php';
include $topdirectory.'/model/classes/events/SNAPEvent.php';


$xml = simplexml_load_file(dirname(__FILE__).'/../config/config.xml');
$databaseXML = $xml->Database;
$database = new Database($databaseXML->Server,$databaseXML->Database,$databaseXML->Username,$databaseXML->Password);


$GLOBALS['DATABASE'] = new Database($databaseXML->Server,$databaseXML->Database,$databaseXML->Username,$databaseXML->Password);

$GLOBALS['FILE_SYSTEM_ROOT'] = dirname(dirname(__FILE__));
//echo  $GLOBALS['FILE_SYSTEM_ROOT'];


require_once($GLOBALS['FILE_SYSTEM_ROOT']."/model/classes/utilities/Users.php");
$users = new Users();
$GLOBALS['USERS'] = $users->getUsers();

require_once($GLOBALS['FILE_SYSTEM_ROOT']."/model/classes/utilities/UsersCFSC.php");
$userscfsc = new UsersCFSC();
$GLOBALS['USERSCFSC'] = $userscfsc->getUsers();

require_once($GLOBALS['FILE_SYSTEM_ROOT']."/model/classes/utilities/UserEmails.php");
$useremails = new UserEmails();
$GLOBALS['USEREMAILS'] = $useremails->getUsers();

require_once($GLOBALS['FILE_SYSTEM_ROOT']."/model/classes/utilities/Locations.php");
$locations = new Locations();
$GLOBALS['LOCATIONS'] = $locations->getLocations();

require_once($GLOBALS['FILE_SYSTEM_ROOT']."/model/classes/utilities/Involvees.php");
$involvees = new Involvees();
$GLOBALS['INVOLVEES'] = $involvees->getInvolvees();

require_once($GLOBALS['FILE_SYSTEM_ROOT']."/model/classes/utilities/EventTypes.php");
$eventTypes = new EventTypes();
$GLOBALS['EVENTTYPES'] = $eventTypes->getEventTypes();



$resultset = new EventsResultSet();
$from = new DateTime('now');
$to = new DateTime('+1 year');
$sql = "SELECT * FROM Events AS e ";
$sql .= "INNER JOIN EventInvolvees AS ei ON e.EventID=ei.EventID ";
$sql .= "WHERE e.Start>'".$from->format("Y-m-d")."' ";
$sql .= "AND e.End<'".$to->format("Y-m-d")."' ";
$sql .= "AND e.DeletedBy=0 ";
$sql .= "AND ei.EventInvolveeNameID=16 ";
$sql .= "ORDER BY e.Start";
//echo $sql;
$resultset->setSql($sql);

$events = $resultset->getEvents();


//echo "hello again";

$messageHTML = "";
$messageHTML .= "<h1>Forthcoming Community Forum Events</h1>\n";
$messageHTML .= "<p>".$from->format("F j, Y")."</p>\n";



$messageHTML .= "<table class=\"locationsList\">";
foreach($GLOBALS['LOCATIONS'] as $location)
{
	$messageHTML .= "<tr class=\"locationsList\"><td width=\"30px\">&nbsp</td><td class=\"locationsList\">".$location['ShortName']."</td><td class=\"locationsList\">".$location['LongName']."</td></tr>\n";
}
$messageHTML .= "</table><br><br>";	


$messageHTML .= "<table>\n";

$previousDate = new DateTime("-10 years");

$users= new Enumeration($database,'SnapUsers');
$array = $users->getArray();
//print_r($array);



foreach ($events as $event) 
{
//	$start = new datetime($event->getStart());
	$start = $event->getStart();
	if($start->format("Y")!=$previousDate->format("Y"))$messageHTML .= printYear($start);
	if($start->format("Ym")!=$previousDate->format("Ym"))$messageHTML .= printMonth($start);
	if($start->format("Ymd")!=$previousDate->format("Ymd"))$messageHTML .= printDay($start);
	$previousDate = $start;	
	$startString = $start->format("H:i");
	
	$messageHTML .= "<tr><td width=\"20px\"></td><td>".$startString."</td><td>".$event->getTitle()."</td><td>".$event->getLocationsShortList()."</td><td>".$event->getDescription()."</td></tr>\n";
}
$messageHTML .= "</table>\n";

$message = new Message();

$recipients = "";

foreach($GLOBALS['USERSCFSC'] as $users) 
{
	echo $users['email']."<br>\n";
	$message->addRecipient($users['email']);
	$recipients .= $users['email']."<br>\n";
}


echo $messageHTML."<br><br>\n\n";

$message->addRecipient("robyn.frame@stnicholascathedral.co.uk");
$message->addRecipient("michael.stoddart@stnicholascathedral.co.uk");

//print_r ($GLOBALS['USERSCFSC'] );

$message->setSubject("Community Forum Events - ".$from->format("F j, Y"));
$message->setFrom("St Nicholas Activity Planning<no-reply@stnicholascathedralmusic.co.uk>");
$message->addStyle("p, td {font-family: sans-serif;vertical-align:top;font-size: 70%;color:#003366;}");
$message->addStyle("h1{font-family: sans-serif;font-size: 100%;color:#003366;}");
$message->addStyle("h2{font-family: sans-serif;font-size: 80%;color:#003366;}");
$message->addStyle("td.EventReportYear{font-family: sans-serif;font-size: 120%;font-weight: 400; color:#003366;}");
$message->addStyle("td.EventReportMonth{font-family: sans-serif;font-size: 110%;font-weight: 400; color:#003366;}");
$message->addStyle("td.EventReportDay{font-family: sans-serif;font-size: 80%;font-weight: 400; color:#003366;}");
$message->addStyle("table.locationsList {color: #332233; font-weight: 100; padding: 0px; margin: 0px;}");
$message->addStyle("tr.locationsList {color: #332233; font-weight: 100; padding: 0px; margin: 0px;}");
$message->addStyle("td.locationsList {color: #332233; font-weight: 100; padding: 0px; margin: 0px;}");

$message->setMessageBody($messageHTML);
//$message->send();

$message = new Message();
$message->addRecipient("michael.stoddart@stnicholascathedral.co.uk");
$message->setSubject("Confirmation - Community Forum Events - ".$from->format("F j, Y"));
$message->setFrom("St Nicholas Activity Planning<no-reply@stnicholascathedralmusic.co.uk>");
$message->addStyle("p, td {font-family: sans-serif;vertical-align:top;font-size: 70%;color:#003366;}");
$message->addStyle("h1{font-family: sans-serif;font-size: 100%;color:#003366;}");
$message->addStyle("h2{font-family: sans-serif;font-size: 80%;color:#003366;}");
$message->addStyle("td.EventReportYear{font-family: sans-serif;font-size: 120%;font-weight: 400; color:#003366;}");
$message->addStyle("td.EventReportMonth{font-family: sans-serif;font-size: 110%;font-weight: 400; color:#003366;}");
$message->addStyle("td.EventReportDay{font-family: sans-serif;font-size: 80%;font-weight: 400; color:#003366;}");
$message->addStyle("table.locationsList {color: #332233; font-weight: 100; padding: 0px; margin: 0px;}");
$message->addStyle("tr.locationsList {color: #332233; font-weight: 100; padding: 0px; margin: 0px;}");
$message->addStyle("td.locationsList {color: #332233; font-weight: 100; padding: 0px; margin: 0px;}");

$message->setMessageBody($recipients."<br>\n".$messageHTML);
$message->send();



